﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.ParentForm;
using UI.Lib;
using UI.Lib.Factory;
using System.Collections;
using Components;

namespace UI.CommonForm
{
    public partial class FrmChangePwd : BaseForm
    {
        public FrmChangePwd()
        {
            InitializeComponent();

            PageInit();
        }

        /// <summary>
        /// 页面初始化时候加载
        /// </summary>
        public void PageInit()
        {
            this.usercode.Text = Context.LoginName;
            this.oldpwd.Focus();
        }

        private void FrmChangePwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                this.tspBtnAdd.PerformClick();
            }
            else if (e.KeyCode == Keys.PageUp)
            {
                this.Close();
            }
        }

        private void tspBtnAdd_Click(object sender, EventArgs e)
        {
            if (!Check()) return;

            Hashtable data = new Hashtable();
            data["pwd"] = Components.SecurityEncryption.StringToMD5Hash(this.newpwd.Text);
            data["oldpwd"] = Components.SecurityEncryption.StringToMD5Hash(this.oldpwd.Text);

            // 将子页面中返回的数据和表格控件中的数据合并生成jSON字符串，返回服务器处理
            string json = StringUtil.Entity.TableToJson(data);

            string errmsg = CommonHelper.Entity.Save("Sys0a00Manager$UpdatePwd", json, this.Action); //调用服务器的保存方法

            bool isSuccess = string.IsNullOrEmpty(errmsg); // 是否保存成功
            if (!isSuccess) MessageBox.Show(errmsg, "提示");
            else
            {
                MessageBox.Show("密码修改成功，记得下次用新密码登录哦！", "提示");
                this.Close();
            }
        }

        /// <summary>
        /// 对界面上的元素进行验证
        /// </summary>
        /// <returns></returns>
        private bool Check()
        {
            if (string.IsNullOrEmpty(oldpwd.Text))
            {
                MessageBox.Show("[原密码]必须输入！", "提示");
                this.oldpwd.Focus();
                return false;
            }
            else if (string.IsNullOrEmpty(newpwd.Text))
            {
                MessageBox.Show("[新密码]必须输入！", "提示");
                this.newpwd.Focus();
                return false;
            }
            else if (string.IsNullOrEmpty(newpwd2.Text))
            {
                MessageBox.Show("[重复新密码]必须输入！", "提示");
                this.newpwd2.Focus();
                return false;
            }
            else if (newpwd.Text != newpwd2.Text)
            {
                MessageBox.Show("[新密码]和[重复新密码]不一致！", "提示");
                this.newpwd2.Focus();
                this.newpwd2.SelectAll();
                return false;
            }

            Hashtable tb = new Hashtable();
            tb["usercode"] = Context.LoginName;
            tb["userpwd"] = Components.SecurityEncryption.StringToMD5Hash(this.oldpwd.Text);

            //验证原密码
            if (!CommonHelper.Entity.DataInData("Sys0a00", tb))
            {
                MessageBox.Show("[原密码]不正确！");
                return false;
            }
            tb = null;

            return true;
        }

        private void newpwd2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                SendKeys.Send("{Tab}");
                e.Handled = true;
            }
        }

        private void tspBtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
